package wwy.Service;

import jakarta.transaction.Transactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import wwy.Common.LogicService;
import wwy.Dao.MerchantDao;
import wwy.Dto.MerchantDto;
import wwy.entity.Merchant;

import java.util.ArrayList;
import java.util.List;

@Service
public class MerchantService extends LogicService<MerchantDao, Merchant, Long> {
    public MerchantService(@Autowired MerchantDao dao){super(dao);}

    @Transactional
    public MerchantDto getMerchantByName(String merchantName){
        Merchant  merchant = dao.findByName(merchantName);
        return modelMapper.map(merchant, MerchantDto.class);
    }
    @Transactional
    public Merchant getMerchantByMerchantId(Long id){
        return dao.findById(id).orElse(null);
    }
    @Transactional
    public List<MerchantDto> getAllMerchants(){
        List<Merchant>  merchants = getAll();
        List<MerchantDto> merchantDtoS = new ArrayList<>();
        for (Merchant merchant : merchants) {
            merchantDtoS.add(modelMapper.map(merchant, MerchantDto.class));
        }
        return merchantDtoS;
    }
    @Transactional
    public void saveMerchants(List<Merchant> merchants){
        dao.saveAll(merchants);
    }

}
